<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if($_RQ['realname']){
		$paramsg=array('uniacid'=>$_SESSION['uniacid']);
		$paramsg['realname like']='%'.$_RQ['realname'].'%';
		$members=pdo_getall('core_members',$paramsg,array('id'),'id');
		if($members){
			$mids=array_keys($members);
		}else{
			$mids=-1;
		}
		$params['mid']=$mids;
		$return['params']['realname']=$_RQ['realname'];
	}
	$list=pdo_getslice('jy_mayitwc_member',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($list as &$item){
		$item['realname']=pdo_getcolumn('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'realname');
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'客户列表'),
			array('op'=>'edit','title'=>'添加上链客户'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'客户ID','type'=>'string'),
			array('name'=>'title','title'=>'客户名称','type'=>'string'),
			array('name'=>'status','type'=>'radio','title'=>'状态','options'=>'islian'),
		),'action'=>array(
			//array('type'=>'post','op'=>'up','title'=>'点击上链'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'title'=>'客户姓名','name'=>'realname'),
		),
	);
	$return['islian']=$this->islian;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id = intval($_RQ['id']);
	if($id){
		$return['item']=pdo_get('jy_mayitwc_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$return['item']=array();
	}
	$return['item']['biz_content']=json_decode($return['item']['biz_content'],true);
	$return['item']['response_data']=json_decode($return['item']['response_data'],true);
	$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'上链客户',
			'tabs'=>array(
				array('op'=>'list','title'=>'返回客户列表'),
			),
			'edit'=>array(
				array('name'=>'orderid','title'=>'选择订单','type'=>'choose','action'=>'/get_order/'),
				array('type'=>'select2','title'=>'一级行业','name'=>'mainclass','options'=>'mainclass','name2'=>'subclass','title2'=>'二级行业','options2'=>'subclass'),
				array('name'=>'productBrand','title'=>'客户品牌','type'=>'string'),
				array('name'=>'productModel','title'=>'客户规格','type'=>'string'),
				array('name'=>'depositPrice','title'=>'免押金额','type'=>'unitstring','unit'=>'元'),
			)
	);
	$return['mainclass']=$this->mainclass;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		if(!$postdata['orderid']){
			exi('请选择订单！','error');
		}
		$isadd=pdo_get('jy_mayitwc_member',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$postdata['orderid']));
		if($isadd['orderid'] && $isadd['orderid']!=$postdata['orderid']){
			exi('该客户已经上过链！！','error');
		}
		if(!$this->setting['aliapp_id']){
			exi('请设置上链支付宝小程序（基础设置--选择上链支付宝小程序）！','error');
		}
		$this->model('member')->up_member($postdata);
		exi('上链成功！');
}
